//
//  SHStatusOriginalView.swift
//  weibo1
//
//  Created by TownFish on 16/7/31.
//  Copyright © 2016年 TownFish. All rights reserved.
//

import UIKit
//设置UIView添加到Cell上
class SHStatusOriginalView: UIView {
    
    //定义一个ViewModel供外界调用
    var statusViewModel: SHStatusViewModel? {
        didSet{
            //设置头像
            headImageView.sh_setImage(statusViewModel?.status?.user?.profile_image_url, placehoderImageName: "avatar_default")
            //昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            
            //时间和来源后续处理(假数据)
            timeLabel.text = "时间"
            sourceLabel.text = "来自 上海微博"
            
            //微博会员等级
            memberImageView.image = statusViewModel?.memberImage
            
            //微博认证用户
            avatarImageView.image = statusViewModel?.avatarImage
            
            //测试
            contentLabel.text = "来自上海微博上海微博上海微博上海微博上海微博上海微博上海微博上号微博上海微博上海微博上海微博上海微博上海微博上海微博"
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI() {
        backgroundColor = RandomColor()
        
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        
        
        
        
        //01
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        //02
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        //03
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing)
            make.centerY.equalTo(nameLabel)
        }
        //04
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        //05
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        //06
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(headImageView)
        }
        //要给原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(10)
        }
        

        //测试数据
        nameLabel.text = "昵称"
        timeLabel.text = "时间"
        sourceLabel.text = "来自 上海微博"
        
    }
    
    //MARK: - 懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15)
    // 微博等级common_icon_membership
    private lazy var memberImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //微博时间
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    //认证log avatar_vgirl
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博内容
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth:  SCREENH - 20)
}

extension SHStatusOriginalView {
    //当前方法已经抽取到ViewModel中
    
    //处理微博认证用户图片
    // 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func getAvatarImage(verified: Int?) -> UIImage? {
        print("处理微博认证用户图片")
        //判断他是否为nil
        guard let ver = verified else {
            return nil
        }
        switch ver {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    
    
    
    
    //处理会员等级 会员等级 1-6
    func getMemberImage(mbrank: Int?) ->UIImage? {
        print("处理会员等级图片")
        //判断他是否为nil
        guard let mbr = mbrank else {
            return nil
        }
        if mbrank > 0 && mbr < 7{
            return UIImage(named: "common_icon_membership_level\(mbr)")
        }
        return UIImage(named: "common_icon_membership")
    }

}
